package com.vitalfit.service.serviceImpl;

import com.vitalfit.model.dto.FoodCustomizationDTO;
import com.vitalfit.model.entity.food.FoodCustomization;
import com.vitalfit.model.entity.food.FoodItem;
import com.vitalfit.repository.FoodCustomizationRepository;
import com.vitalfit.repository.FoodItemRepository;
import com.vitalfit.service.FoodCustomizationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class FoodCustomizationServiceImpl implements FoodCustomizationService {

    private final FoodCustomizationRepository foodCustomizationRepository;
    private final FoodItemRepository foodItemRepository;

    /**
     * 根据食品ID查询所有相关的食品定制选项
     * @param foodItemId 食品ID
     * @return 食品定制选项列表
     */
    @Override
    @Transactional(readOnly = true)
    public List<FoodCustomization> findCustomizationsByFoodItemId(Integer foodItemId) {
        return foodCustomizationRepository.findAllByFoodItemId(foodItemId);
    }

    /**
     * 创建新的食品定制项
     * @param foodCustomizationDTO 食品定制数据传输对象
     * @return 新创建的食品定制项
     */
    @Override
    @Transactional
    public FoodCustomization createCustomization(FoodCustomizationDTO foodCustomizationDTO) {
        FoodItem foodItem = foodItemRepository.findById(foodCustomizationDTO.getFoodItemId())
                .orElseThrow(() -> new IllegalArgumentException("Food not found: " + foodCustomizationDTO.getFoodItemId()));

        FoodCustomization foodCustomization = new FoodCustomization();
        foodCustomization.setCustomizationType(foodCustomizationDTO.getCustomizationType());
        foodCustomization.setFoodItem(foodItem);
        foodCustomization.setAdditionalPrice(foodCustomizationDTO.getAdditionalPrice());
        return foodCustomizationRepository.save(foodCustomization);
    }

    /**
     * 更新现有的食品定制项
     * @param foodCustomizationDTO 食品定制数据传输对象
     * @return 更新后的食品定制项
     */
    @Override
    @Transactional
    public FoodCustomization updateCustomization(FoodCustomizationDTO foodCustomizationDTO) {
        FoodCustomization existing = foodCustomizationRepository.findById(foodCustomizationDTO.getId())
                .orElseThrow(() -> new IllegalArgumentException("Food customization not found: " + foodCustomizationDTO.getId()));

        existing.setCustomizationType(foodCustomizationDTO.getCustomizationType());
        existing.setAdditionalPrice(foodCustomizationDTO.getAdditionalPrice());
        return foodCustomizationRepository.save(existing);
    }

    /**
     * 根据ID删除食品定制项
     * @param id 食品定制项ID
     */
    @Override
    @Transactional
    public void deleteCustomization(Integer id) {
        if (!foodCustomizationRepository.existsById(id)) {
            throw new IllegalArgumentException("Food customization not found: " + id);
        }
        foodCustomizationRepository.deleteById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public FoodCustomization getCustomizationById(Integer id) {
        return foodCustomizationRepository.findById(id).orElseThrow(
                () ->  new IllegalArgumentException("Food customization not found: " + id)
        );
    }
}
